// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * Support for dwc3 platform devices for lombo platforms
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/dma-mapping.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/of_device.h>
#include <linux/regmap.h>
#include <linux/mfd/syscon.h>

struct dwc3_lb {
	struct device		*dev;
	int			num_clk;
	struct clk		**clks;
};

static int dwc3_lb_init_clks(struct dwc3_lb *lb, struct device_node *np)
{
	int		i;
	struct clk	*clk;
	struct device	*dev = lb->dev;

	lb->num_clk = of_clk_get_parent_count(np);
	if (!lb->num_clk) {
		dev_err(dev, "failed to get dwc3 lb clk resource\n");
		return -ENOENT;
	}

	lb->clks = devm_kcalloc(dev, lb->num_clk,
				sizeof(struct clk *), GFP_KERNEL);
	if (!lb->clks)
		return -ENOMEM;

	for (i = 0; i < lb->num_clk; i++) {
		clk = of_clk_get(np, i);
		if (IS_ERR(clk)) {
			dev_err(dev, "failed to get dwc3 lb clk\n");
			while (--i >= 0)
				clk_put(lb->clks[i]);

			return PTR_ERR(clk);
		}

		lb->clks[i] = clk;
	}

	return 0;
}

static int dwc3_lb_enable_clks(struct dwc3_lb *lb)
{
	int i;
	int ret;

	for (i = 0; i < lb->num_clk; i++) {
		ret = clk_prepare_enable(lb->clks[i]);
		if (ret < 0) {
			dev_err(lb->dev, "failed to enable dwc3 lb clk\n");
			while (--i >= 0)
				clk_disable_unprepare(lb->clks[i]);

			return ret;
		}
	}

	return 0;
}

static int dwc3_lb_probe(struct platform_device *pdev)
{
	struct dwc3_lb		*lb;
	struct device		*dev = &pdev->dev;
	struct device_node	*np = pdev->dev.of_node;
	int			ret;
	int			i;

	lb = devm_kzalloc(&pdev->dev, sizeof(struct dwc3_lb), GFP_KERNEL);
	if (!lb)
		return -ENOMEM;

	platform_set_drvdata(pdev, lb);
	lb->dev = &pdev->dev;

	ret = dwc3_lb_init_clks(lb, np);
	if (ret)
		return ret;

	ret = dwc3_lb_enable_clks(lb);
	if (ret)
		goto err_put_clks;

	ret = of_platform_populate(np, NULL, NULL, dev);
	if (ret) {
		dev_err(dev, "failed to register dwc3 core %d\n", ret);
		goto err_disable_clks;
	}

	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);
	pm_runtime_get_sync(dev);

	return 0;

err_disable_clks:
	for (i = 0; i < lb->num_clk; i++)
		clk_disable_unprepare(lb->clks[i]);

err_put_clks:
	for (i = 0; i < lb->num_clk; i++)
		clk_put(lb->clks[i]);

	return ret;
}

static int dwc3_lb_remove(struct platform_device *pdev)
{
	struct dwc3_lb	*lb = platform_get_drvdata(pdev);
	struct device	*dev = &pdev->dev;
	int		i;

	for (i = 0; i < lb->num_clk; i++) {
		clk_disable_unprepare(lb->clks[i]);
		clk_put(lb->clks[i]);
	}

	pm_runtime_put_sync(dev);
	pm_runtime_disable(dev);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int dwc3_lb_suspend(struct device *dev)
{
	struct dwc3_lb *lb = dev_get_drvdata(dev);
	int i;

	for (i = 0; i < lb->num_clk; i++)
		clk_disable_unprepare(lb->clks[i]);

	/* TODO! depend on platform */

	return 0;
}

static int dwc3_lb_resume(struct device *dev)
{
	struct dwc3_lb *lb = dev_get_drvdata(dev);
	int ret;

	ret = dwc3_lb_enable_clks(lb);
	if (ret) {
		dev_err(dev, "%s enable clk failed\n", __func__);
		return ret;
	}

	/* TODO! depend on platform */

	return 0;
}

static const struct dev_pm_ops dwc3_lb_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(dwc3_lb_suspend, dwc3_lb_resume)
};

#define DEV_PM_OPS	(&dwc3_lb_dev_pm_ops)
#else
#define DEV_PM_OPS	NULL
#endif /* CONFIG_PM_SLEEP */

static const struct of_device_id dwc3_lb_match[] = {
	{ .compatible = "lombo,n9-dwc3-usb2drd0" },
	{ .compatible = "lombo,n9-dwc3-usb2drd1" },
	{ .compatible = "lombo,n9-dwc3-usb2host0" },
	{ .compatible = "lombo,n9-dwc3-usb3drd0" },
	{ .compatible = "lombo,n9-dwc3" },
	{},
};

MODULE_DEVICE_TABLE(of, dwc3_lb_match);

static struct platform_driver dwc3_lb_driver = {
	.probe		= dwc3_lb_probe,
	.remove		= dwc3_lb_remove,
	.driver		= {
		.name	= "lombo-dwc3",
		.of_match_table = dwc3_lb_match,
		.pm	= DEV_PM_OPS,
	},
};

module_platform_driver(dwc3_lb_driver);

MODULE_ALIAS("platform:lb-usb");
MODULE_AUTHOR("lomboswer@lombotech.com");
MODULE_DESCRIPTION("Driver for LomboTech usb3");
MODULE_LICENSE("GPL");
